package com.esig.Model;

import com.esig.View.GameFrame;
import com.esig.util.Direction;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Bullet extends GameObject {
    //打印到目的地的左上和右下坐标
    public int dx1;
    public int dy1;
    public int dx2;
    public int dy2;

    //从来源地复制的左上和右下坐标
    public int sx1;
    public int sy1;
    public int sx2;
    public int sy2;

    //速度
    private int speed = 7;
    //攻击
    public int attack = 5;
    //方向
    Direction direction;
    //swing计时器 子弹图片
    public Timer ImgTimer;
    //全局的变量，用来当作图片切换计时器的参数
    public String imgTimerParameter = "右子弹更改图片";
    //子弹是否可见
    private boolean visible = true;


    //构造函数 带精灵图
    public Bullet(String img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Direction direction, GameFrame gameFrame) {
        super(img, dx1, dy1, gameFrame);
        this.dx1 = dx1;
        this.dy1 = dy1;
        this.dx2 = dx2;
        this.dy2 = dy2;
        this.sx1 = sx1;
        this.sy1 = sy1;
        this.sx2 = sx2;
        this.sy2 = sy2;
        this.direction = direction;
    }

    //子弹对象池初始化
    public Bullet() {
    }

    public void go() {
        switch (direction) {
            case LEFT:
                leftward();
                break;
            case RIGHT:
                rightward();
                break;
        }
    }

    public void leftward() {
        if (dx2 < 0) {   //左边时dx2是屁股
            visible = false;
            imgOnEnd();
        } else {
            if (imgTimerParameter != "左子弹消失更改图片") {
                dx1 -= speed;
                dx2 -= speed;
                imgTimerParameter = "左子弹更改图片";
                imgOnStart();
            }

        }

    }

    private boolean isTwo = false; // 判断是否阶段二

    public void rightward() {
        if (dx1 > 900) {    //右边时dx1是屁股
            visible = false;
            imgOnEnd();
        } else {
            if (imgTimerParameter != "右子弹消失更改图片") {
                dx1 += speed;
                dx2 += speed;
                imgTimerParameter = "右子弹更改图片";
                imgOnStart();
            }
        }
    }

    //子弹的图片切换动作
    public void leftChangeImg() {
        if (isTwo) {   //子弹切换图片的 阶段二
            if (sx1 == 240) {
                sx1 = 160;
                sx2 = 240;
                //这里到时候搞一个，奇数气功波稍微向上，偶数气功波稍微向下
            } else {
                sx1 = 240;
                sx2 = 320;
            }
        } else {
            sx1 -= 80;
            sx2 -= 80;
            if (sx1 <= 0) {
                isTwo = true;
                sx1 = 240;
                sx2 = 320;
                //切记子弹的宽高是 80 * 45
                sy1 = 90;
                sy2 = 135;
            }
        }
    }

    public void rightChangeImg() {
        if (isTwo) {
            if (sx1 == 0) {
                sx1 = 80;
                sx2 = 160;
                //这里到时候搞一个，奇数气功波稍微向上，偶数气功波稍微向下
            } else {
                sx1 = 0;
                sx2 = 80;
            }
        } else {
            sx1 += 80;
            sx2 += 80;
            if (sx1 >= 320) {
                isTwo = true;
                sx1 = 0;
                sx2 = 80;
                //切记子弹的宽高是 80 * 45
                sy1 = 90;
                sy2 = 135;
            }
        }
    }

    public void leftDie() {
        sy1 = 45;
        sy2 = 90;

        sx1 -= 80;
        sx2 -= 80;
        if (sx1 == 0) {  //子弹消失的操作
            visible = false;
            imgOnEnd();
        }
    }

    public void rightDie() {
        sy1 = 45;
        sy2 = 90;

        sx1 += 80;
        sx2 += 80;
        if (sx1 == 240) { //子弹消失的操作
            visible = false;
            imgOnEnd();
        }
    }


    /************ 计时器实现子弹图片150ms左右切换 start *******************/
    private class Img implements ActionListener {

        private int jumpP = 0;
        private int jumpE = 80;

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            //1、actionPerformed()在界面工作线程里运行，所以必须迅速返回
            //2、在这里可以直接更新UI
            if (imgTimerParameter == "左子弹更改图片") {
                leftChangeImg();
            }
            if (imgTimerParameter == "右子弹更改图片") {
                rightChangeImg();
            }
            if (imgTimerParameter == "左子弹消失更改图片") {
                leftDie();
            }
            if (imgTimerParameter == "右子弹消失更改图片") {
                rightDie();
            }
//            System.out.println("开始子弹图片切换定时器");
        }

    }

    //开始定时器
    public void imgOnStart() {
        if (ImgTimer != null) return;

        ActionListener task = new Img();
        if (imgTimerParameter == "左子弹更改图片") {
            ImgTimer = new Timer(150, task);
        }
        if (imgTimerParameter == "右子弹更改图片") {
            ImgTimer = new Timer(150, task);
        }
        if (imgTimerParameter == "左子弹消失更改图片") {
            ImgTimer = new Timer(150, task);
        }
        if (imgTimerParameter == "右子弹消失更改图片") {
            ImgTimer = new Timer(150, task);
        }

        ImgTimer.start();

    }

    //停止定时器
    public void imgOnEnd() {
        if (ImgTimer != null) {
            ImgTimer.stop();
            ImgTimer = null;
        }
        System.out.println("关闭图片切换定时器");
    }

    /************ 计时器实现子弹图片150ms左右切换 end *******************/


    @Override
    public void paintSelf(Graphics g) {
        if (!visible) return;
        g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
        go();
    }

    @Override
    public Rectangle getRec() {
        return new Rectangle(dx1, dy1, 40, dy2 - dy1);
    }

    public boolean getVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }
}
